﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnnamedPodcastLibrary;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;

namespace PodcastDownloader
{
    [DebuggerDisplay("Text")]
    internal class LogMessage
    {
        ManagerChangedEventArgs Event { get; set; }

        public LogMessage(ManagerChangedEventArgs args)
        {
            this.Time = DateTime.Now;
            this.Event = args;
        }
        
        public string Message { get { return this.Event.Job.Message; } }
        public DateTime Time { get; set; }
        //public string Download { 
        //    get 
        //    {
        //        if (this.Event.Episode != null
        //            && !string.IsNullOrEmpty(this.Event.Episode.Title))
        //            return string.Format("{0} {1}", this.Event.Episode.Title, this.Event.Episode.FeedTitle ?? "feed unknown");
        //        if(this.Event.File != null)
        //            return this.Event.File.ToString();
        //        return string.Empty;
        //    } 
        //}
        //
        //public string Text 
        //{ 
        //    get 
        //    {
        //        switch (this.Event.Kind)
        //        {
        //            case ManagerEventKind.FileDownloadQueued:
        //                return "File download queued.";
        //            case ManagerEventKind.SyncFileAvailable:
        //                return "File available to sync.";
        //            case ManagerEventKind.Started:
        //                return "Podcast manager started.";
        //            case ManagerEventKind.Stopped:
        //                return "Podcast manager stopped.";
        //            case ManagerEventKind.FatalError:
        //                return "!! Fatal Error !!";
        //            case ManagerEventKind.Error:
        //                return "Error: " + this.Event.Error;
        //            case ManagerEventKind.Warning:
        //                return "Warning: " + this.Event.Error; 
        //            case ManagerEventKind.FileDownloading:
        //                return "Downloading file";
        //            case ManagerEventKind.FileDownloaded:
        //                return "Downloaded file";
        //            case ManagerEventKind.JobQueued:
        //                return this.Event.Message;
        //            case ManagerEventKind.SyncFileSuccess:
        //                return "Sync file success";
        //            case ManagerEventKind.SyncFileFailure:
        //                return "Sync file failure";
        //            case ManagerEventKind.SyncFinishedSuccess:
        //                return "Sync job completed successfully";
        //            case ManagerEventKind.SyncFinishedFailure:
        //                return "Sync job aborted.";
        //            default:
        //                return string.Empty;
        //        }
        //    } 
        //}

    }
   
    [DebuggerDisplay("{FileName}")]
    internal class DownloadMessage : IEquatable<DownloadMessage>
    {
        public string FileName { get; set; }
        #region IEquatable<DownloadMessage> Members

        public bool Equals(DownloadMessage other)
        {
            return this.FileName.Equals(other.FileName, StringComparison.OrdinalIgnoreCase);
        }

        #endregion
    }

    [DebuggerDisplay("{Title}")]
    internal class EpisodeModel : IEquatable<EpisodeModel>
    {
        public EpisodeModel(Episode episode)
        {
            if (episode == null)
                throw new ArgumentNullException("episode");
            this.Episode = episode;
            this.Title = episode.Title;
        }
        public string Title { get; private set; }
        public string Description { get { return this.Episode.Description; } }
        public string Feed { get { return this.Episode.FeedTitle; } }
        public string Size { get {return MainForm.ToMegaByte(this.Episode.FileSize);}}
        
        Episode Episode { get; set; }
        public bool Equals(EpisodeModel other)
        {
            return this.Episode.UniqueId.Equals(other.Episode.UniqueId, StringComparison.OrdinalIgnoreCase);
        }
    }

    [DebuggerDisplay("{Title}")]
    internal class FeedModel : IEquatable<FeedModel>, IDisposable
    {
        public FeedModel(Feed feed)
        {
            if (feed == null)
                throw new ArgumentNullException("feed");
            this.Feed = feed;
            this.Title = feed.Title;
            this.Episodes = feed.Episodes;
            if(!string.IsNullOrEmpty(feed.ImageLocalFileName)
                && File.Exists(feed.ImageLocalFileName))
            {
                this.Image = ResizeImage(Image.FromFile(feed.ImageLocalFileName), new Size(100,100));
            }
        }
       
        public string Title { get; private set; }
        public string Description { get { return this.Feed.Description; } }
        public Image Image { get; private set; }
        Feed Feed { get; set; }
        public List<Episode> Episodes { get; private set; }
        public bool Equals(FeedModel other)
        {
            return this.Feed.DownloadUrl.OriginalString.Equals(other.Feed.DownloadUrl.OriginalString, StringComparison.OrdinalIgnoreCase);
        }
        private Image ResizeImage(Image original, Size newSize)
        {
            var bitmap = new Bitmap(newSize.Width, newSize.Height);
            using (var newGrapics = Graphics.FromImage(bitmap))
            {
                newGrapics.CompositingQuality = CompositingQuality.HighSpeed;
                newGrapics.SmoothingMode = SmoothingMode.HighSpeed;
                newGrapics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                newGrapics.DrawImage(original, new Rectangle(0, 0, newSize.Width, newSize.Height));
            }

            return bitmap;
        }

    
#region IDisposable Members

public void  Dispose()
{
 	if(this.Image != null)
        this.Image.Dispose();
}

#endregion
}
}
